package Book;
import Constant.Constant;
import utils.AnalyzingBook;

import java.io.IOException;

public class Library {
    private Book[] books;
    private int bookCount;
    private AnalyzingBook analyzingBook;

    public Library(){
       // books= new Book[Constant.CAPACITY];
        loadAllBook();
    }
    private void loadAllBook() {

//1. 读取⽂件内容
            Book[] allBook = analyzingBook.loadObject(Constant.ALL_BOOK_FILE_NAME);
//默认⼤⼩为5
            books = new Book[Constant.CAPACITY];
//2. 是否有数据 没有数据 有效书籍个数为 0
            if(allBook == null) {
                bookCount = 0;
            }else {
//3. 查看实际书籍⻓度是多少 是否⼤于默认的⻓度5
                int allBookLen = allBook.length;
//3.1 ⼤于默认⻓度 books数组 分配实际的⼤⼩
                if (allBookLen > books.length) {
                    books = new Book[allBookLen];
                }
//3.2 把读到的元素进⾏赋值
                for (int i = 0; i < allBookLen; i++) {
                    books[i] = allBook[i];
                }
//4.修改实际有效书籍个数
                bookCount = allBookLen;
            }
        }

    private void storeBook() {

        analyzingBook.storeObject(books, Constant.ALL_BOOK_FILE_NAME);

    }
}